﻿



namespace Orid.Management.SOHO.Book.Client.UI
{

    using System;
    using System.Collections;
    using System.Windows.Forms;
    using Orid.Management.SOHO.Book.Client.Logic;

    public partial class FormProceeds : Form
    {
        public FormProceeds()
        {
            _utilScoreResult =new UtilScoreResult(@"192.168.161.159");
            loginName = "test01";

            InitializeComponent();

            InitList();
        }

        private UtilScoreResult _utilScoreResult = null;
        private string loginName;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="proxy">SOHOProxy 实例</param>
        /// <param name="loginName">录入员ID</param>
        public FormProceeds(Orid.Management.Component.SOHOProxy proxy,string loginName)
        {
            InitializeComponent();
            _utilScoreResult = new UtilScoreResult(proxy);
            this.loginName = loginName;
            InitList();
        }

        private void InitList()
        {
            lvwColumnSorter = new ListViewColumnSorter();
            this.listView1.ListViewItemSorter = lvwColumnSorter;
            listView1.ColumnClick -= new ColumnClickEventHandler(listViewDecompositionOrders_ColumnClick);
            listView1.ColumnClick += new ColumnClickEventHandler(listViewDecompositionOrders_ColumnClick);


            if (radioButtonJobClass.Checked)
            {
                InitListJobClass();
                FillListJobClass();
                listView1.Refresh();
            }
            else if (radioButtonDate.Checked)
            {
                InitListDate();
                FillListDate();
                listView1.Refresh();
            }

            listView1.GridLines = true;

          
            
        }

        private void InitListJobClass()
        {
            listView1.Clear();
    
            listView1.Columns.Clear();

            ColumnHeader columnHeader = new ColumnHeader() { Name = "JobClass", Text = @"ジョブ種類", Width = 100, TextAlign = HorizontalAlignment.Center };
            listView1.Columns.Add(columnHeader);

            columnHeader = new ColumnHeader() { Name = "Category", Text = @"カテゴリ", Width = 100, TextAlign = HorizontalAlignment.Center };
            listView1.Columns.Add(columnHeader);

            columnHeader = new ColumnHeader() { Name = "JobCount", Text = @"ジョブ数量", Width = 200, TextAlign = HorizontalAlignment.Right };
            listView1.Columns.Add(columnHeader);

            columnHeader = new ColumnHeader() { Name = "ImgCount", Text = @"画像数量", Width = 200, TextAlign = HorizontalAlignment.Right };
            listView1.Columns.Add(columnHeader);
        }


        private void InitListDate ()
        {
            listView1.Clear();

            listView1.Columns.Clear();

            ColumnHeader columnHeader = new ColumnHeader() { Name = "Date", Text = @"期日", Width = 100, TextAlign = HorizontalAlignment.Center };
            listView1.Columns.Add(columnHeader);

            columnHeader = new ColumnHeader() { Name = "JobClass", Text = @"ジョブ種類", Width = 100, TextAlign = HorizontalAlignment.Center };
            listView1.Columns.Add(columnHeader);

            columnHeader = new ColumnHeader() { Name = "Category", Text = @"カテゴリ", Width = 100, TextAlign = HorizontalAlignment.Center };
            listView1.Columns.Add(columnHeader);

            columnHeader = new ColumnHeader() { Name = "JobCount", Text = @"ジョブ数量", Width = 200, TextAlign = HorizontalAlignment.Right };
            listView1.Columns.Add(columnHeader);

            columnHeader = new ColumnHeader() { Name = "ImgCount", Text = @"画像数量", Width = 200, TextAlign = HorizontalAlignment.Right };
            listView1.Columns.Add(columnHeader);
        }

        private int currentYear
        {
            get { return this.dateTimePicker1.Value.Year; }
        }

        private int currentMonth
        {
            get { return this.dateTimePicker1.Value.Month; }
        }

        private void FillListJobClass()
        {

            Orid.Management.Common.Job.JobStatistics[] listResult = _utilScoreResult.GetUserJobCount(loginName, currentYear, currentMonth);

            ListViewItem lstItem = new ListViewItem();

            lstItem.SubItems.Clear();

            if (listResult != null)
            {
         
                foreach (Orid.Management.Common.Job.JobStatistics jobStatisticse in listResult)
                {
                    lstItem = new ListViewItem();
                    lstItem.SubItems[0].Text = jobStatisticse.JobClassName;
                    lstItem.SubItems.Add(jobStatisticse.ImageType.ToString());
                    lstItem.SubItems.Add(jobStatisticse.JobCount.ToString());
                    lstItem.SubItems.Add(jobStatisticse.Piece.ToString());
                    this.listView1.Items.Add(lstItem);
                }
            }
    }

        private void FillListDate()
        {

            Orid.Management.Common.Job.JobStatistics[] listResult = _utilScoreResult.GetUserJobCountDetail(loginName, currentYear, currentMonth);

            ListViewItem lstItem = new ListViewItem();

            lstItem.SubItems.Clear();

            if (listResult != null)
            {

                foreach (Orid.Management.Common.Job.JobStatistics jobStatisticse in listResult)
                {
                    lstItem = new ListViewItem();
                    lstItem.SubItems[0].Text = jobStatisticse.UploadDate;
                    lstItem.SubItems.Add(jobStatisticse.JobClassName.ToString());
                    lstItem.SubItems.Add(jobStatisticse.ImageType.ToString());
                    lstItem.SubItems.Add(jobStatisticse.JobCount.ToString());
                    lstItem.SubItems.Add(jobStatisticse.Piece.ToString());
                    this.listView1.Items.Add(lstItem);
                }
            }
        }

        private void buttonSearch_Click(object sender, EventArgs e)
        {
            InitList();
        }

        void listViewDecompositionOrders_ColumnClick(object sender, ColumnClickEventArgs e)
        {
            if (e.Column == lvwColumnSorter.SortColumn)
            {
                if (lvwColumnSorter.Order == System.Windows.Forms.SortOrder.Ascending)
                {
                    lvwColumnSorter.Order = System.Windows.Forms.SortOrder.Descending;
                }
                else
                {
                    lvwColumnSorter.Order = System.Windows.Forms.SortOrder.Ascending;
                }
            }
            else
            {
                lvwColumnSorter.SortColumn = e.Column;
                lvwColumnSorter.Order = System.Windows.Forms.SortOrder.Ascending;
            }
            this.listView1.Sort();

        }
        #region liview排序定义

        private ListViewColumnSorter lvwColumnSorter;
        class ListViewColumnSorter : IComparer
        {
            /// <summary>
            /// 指定按照哪个列排序
            /// </summary>
            private int ColumnToSort;
            /// <summary>
            /// 指定排序的方式
            /// </summary>
            private System.Windows.Forms.SortOrder OrderOfSort;
            /// <summary>
            /// 声明CaseInsensitiveComparer类对象，
            /// 参见ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.2052/cpref/html/frlrfSystemCollectionsCaseInsensitiveComparerClassTopic.htm
            /// </summary>
            private CaseInsensitiveComparer ObjectCompare;
            /// <summary>
            /// 构造函数
            /// </summary>
            public ListViewColumnSorter()
            {
                // 默认按第一列排序
                ColumnToSort = 0;
                // 排序方式为不排序
                OrderOfSort = System.Windows.Forms.SortOrder.None;
                // 初始化CaseInsensitiveComparer类对象
                ObjectCompare = new CaseInsensitiveComparer();
            }
            /// <summary>
            /// 重写IComparer接口.
            /// </summary>
            /// <param name="x">要比较的第一个对象</param>
            /// <param name="y">要比较的第二个对象</param>
            /// <returns>比较的结果.如果相等返回0，如果x大于y返回1，如果x小于y返回-1</returns>
            public int Compare(object x, object y)
            {
                int compareResult;
                //当点击Remaining Job Count列的时候 不是用item的text排序 是用MaxJobCount来排序


                ListViewItem listviewX, listviewY;
                // 将比较对象转换为ListViewItem对象
                listviewX = (ListViewItem)x;
                listviewY = (ListViewItem)y;

                // 比较
                try
                {
                    int valueX = int.Parse(listviewX.SubItems[ColumnToSort].Text);
                    int valueY = int.Parse(listviewY.SubItems[ColumnToSort].Text);
                    compareResult = ObjectCompare.Compare(valueX, valueY);
                }
                catch  
                {
                    compareResult = ObjectCompare.Compare(listviewX.SubItems[ColumnToSort].Text, listviewY.SubItems[ColumnToSort].Text);
                    
                }
               
          
                

              
                

                // 根据上面的比较结果返回正确的比较结果
                if (OrderOfSort == System.Windows.Forms.SortOrder.Ascending)
                {
                    // 因为是正序排序，所以直接返回结果
                    return compareResult;
                }
                else if (OrderOfSort == System.Windows.Forms.SortOrder.Descending)
                {
                    // 如果是反序排序，所以要取负值再返回
                    return (-compareResult);
                }
                else
                {
                    // 如果相等返回0
                    return 0;
                }
            }
            /// <summary>
            /// 获取或设置按照哪一列排序.
            /// </summary>
            public int SortColumn
            {
                set
                {
                    ColumnToSort = value;
                }
                get
                {
                    return ColumnToSort;
                }
            }
            /// <summary>
            /// 获取或设置排序方式.
            /// </summary>
            public System.Windows.Forms.SortOrder Order
            {
                set
                {
                    OrderOfSort = value;
                }
                get
                {
                    return OrderOfSort;
                }
            }
        }
        #endregion liview排序定义

        private void buttonClose_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
